package com.kuang.listener;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


public class OnlineCountListener implements HttpSessionListener {
    public void sessionCreated(HttpSessionEvent se) {
//
        ServletContext ctx =se.getSession().getServletContext();
        Integer onlineCount=(Integer) ctx.getAttribute("OnlineCount");
        if (onlineCount==null){
            onlineCount = 1;
        }else {
            onlineCount= onlineCount + 1;
        }
        ctx.setAttribute("OnlineCount",onlineCount);
    }

    public void sessionDestroyed(HttpSessionEvent se) {

        ServletContext ctx =se.getSession().getServletContext();
        Integer onlineCount=(Integer) ctx.getAttribute("OnlineCount");
        if (onlineCount == null ){
            onlineCount = 0;
        }else {
            onlineCount= onlineCount - 1;
        }
        ctx.setAttribute("OnlineCount",onlineCount);
    }

}
